/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Delegate;
import com.bourre.events.EventBroadcaster;
import com.bourre.events.EventType;

import fever.app.menu.FvContextMenu;
import fever.app.menu.FvContextMenuEvent;
import fever.utils.ObjectUtils;
import fever.utils.Stringifier;

/**
 * {@link FvContextMenuItem } class is a wrapper for orignal 
 * Adobe {@code ContextMenuItem} class.
 * 
 * <p>Automatically register item callback to dispatch 
 * {@link fever.app.menu.FvContextMenuEvent#onContextItemEVENT} event. 
 * 
 * <p>{@link FvContextMenuItem } still {@code dynamic}. 
 * 
 * <p>Take a look at {@link fever.app.menu.FvContextMenu} for example.
 * 
 * @see fever.app.menu.FvContextMenu
 * @see fever.app.menu.FvContextMenuHelper
 * 
 * @author Romain Ecarnot
 */
dynamic class fever.app.menu.FvContextMenuItem extends ContextMenuItem
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Dispatched whe user click on context menu item. */
	public static var onContextItemEVENT : EventType = FvContextMenuEvent.onContextItemEVENT;
	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _broadcaster : EventBroadcaster;
	private var _source : String;
	private var _id : String;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Don't use. */
	public var __KEY : Number = null;
	
	/** String used to prefix the label item. */
	public static var PREFIX_ROOT : String = ( chr( 183 ) + ' ' );
	
	
	/** {@link fever.app.menu.FvContextMenu} instance which contain this item. */
	public var contextMenuOwner : FvContextMenu;
	
	/**
	 * Returns item identifier.
	 */
	public function get id() : String { return _id; }
	public function set id ( p : String ) : Void { _id = p;	}
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param label A {@code String} that specifies the menu item caption (text) displayed 
	 * in the context menu.
	 * @param useSeparator (optional) A {@code Boolean} value that indicates whether a separator bar 
	 * should appear above the specified menu item	
	 * @param enabled (optional) A {@code Boolean} value that indicates whether the specified menu item 
	 * is enabled or disabled.
	 * @param visible (optional) A {@code Boolean} value that indicates whether the specified menu item 
	 * is visible when the Flash Player context menu is displayed.
	 */
	public function FvContextMenuItem( label : String, useSeparator : Boolean, enabled : Boolean, visible : Boolean )
	{
		super( PREFIX_ROOT + label, Delegate.create( this, _select ), useSeparator, enabled, visible );
		
		_broadcaster = new EventBroadcaster( this );
		ObjectUtils.protectObject( this );
	}
	
	/**
	 * Sets caption
	 */
	public function setCaption( txt : String ) : Void
	{
		caption = PREFIX_ROOT + txt;
		_source = txt;
	}
	
	/**
	 * Returns item caption
	 */
	public function getCaption() : String
	{	
		return _source;
	}
	
	/**
	 * Adds passed-in {@code listener} for receiving passed-in {@code type} event.
	 * 
	 * @param type Name of the Event.
	 * @param listener Listener object.
	 */
	public function addEventListener( type : EventType, listener ) : Void
	{
		_broadcaster.addEventListener.apply( _broadcaster, arguments );	
	}
	
	/**
	 * Removes passed-in {@code listener} that suscribed for passed-in {@code type} event.
	 * 
	 * @param type Name of the Event.
	 * @param listener Listener object.
	 */
	public function removeEventListener( type : EventType, listener ) : Void
	{
		_broadcaster.removeEventListener( type, listener );
	}
	
	/**
	 * Removes all listeners.
	 */
	public function removeAllListeners() : Void
	{
		_broadcaster.removeAllListeners();	
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this, caption );
	}
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Triggers when item is clicked.
	 * 
	 * <p>Dispatchs {@code fever.app.menu.FvContextMenuEvent#MENU_ITEM_SELECT} event.
	 * 
	 * @param obj mouse target
	 * @param item ContextMenuItem instance
	 */
	private function _select( obj, item : ContextMenuItem ) : Void
	{
		contextMenuOwner.broadcastEvent( new FvContextMenuEvent( FvContextMenuItem.onContextItemEVENT, contextMenuOwner, this ) );
		
		var event : FvContextMenuEvent = new FvContextMenuEvent( FvContextMenuItem.onContextItemEVENT, contextMenuOwner, this );
		event.mouseTarget = obj;
		_broadcaster.broadcastEvent( event );
	}
}